MVC 最佳實踐 | 您是否將鏈接構建到 Url Helper 中? (MVC Best Practices | Do you build your links into the Url Helper?)


問題描述

MVC 最佳實踐 | 您是否將鏈接構建到 Url Helper 中? (MVC Best Practices | Do you build your links into the Url Helper?)

Just read a post about MVC best practices. Couple parts of the post described building helper methods to link to actions on the controllers. Here's a clip:

  

1) Create Extension methods of UrlHelper to generate your url from   Route

     

Avoid passing the controller, action   or route name as string, create   extension methods of UrlHelper which   encapsulates it, for example:

public static class UrlHelperExtension
{
    public static string Home(this UrlHelper helper)
    {
        return helper.Content("~/");
    }

    public static string SignUp(this UrlHelper helper)
    {
        return helper.RouteUrl("Signup");
    } 
}

I can see how this would shorten links used in views... but I don't see how this is a "best practice". Perhaps I'm simply overlooking something. Should I be doing this to build my links? Are there benefits to this I'm just not seeing?

He even goes on to say that stylesheets, images, and javascript helpers should be made...


參考解法

方法 1:

I probably wouldn't do this unless the route is referenced in multiple places.  I can see the value in doing this, and I have implemented HtmlHelper extensions for adding style sheets and javascript includes to add the ability to use "versioned" links, but my common links are included as ActionLinks in a UserControl (menu) and for most other things I use either ActionLink or BeginForm/AjaxForm to reference the action.  In some respects it feels like creating new methods for all but the most commonly reused links is adding complexity for very little value -- the kind of value that you would get from some simple manual testing of your UI, which you'd have to do anyway.

方法 2:

If you make a mistake when writing a URL as a string, it won't be caught until runtime. This way, attempting to refer to a route that hasn't been created as an extension method will create errors at compile-time, which you can quickly correct (even earlier, if you use Visual Studio). If you discover a mistake in the way you formulated a route, you only have to fix it in one place.

Although, instead of cluttering your UrlHelpers with extension methods, it might be better to have a static class called something like CommonUrls that holds static readonly properties (or static methods, if you prefer).

EDIT: I just realized that you would need to pass an instance of your UrlHelper to the CommonUrls class. Silly me. In that case, extension methods probably are the right tool for the job.

方法 3:

That's one person's opinion of what a best practice is. There are a few cases where it might be nice. If you have people working with the views while the URL structure is still being worked out you won't have to update the views once the URLs are finalized.

方法 4:

Should anyone else happen to stumble on this old post, I would suggest using the T4MVC templates by David Ebbo: See his latest blog post here.

方法 5:

I don't see this as a best practice. For one thing you're going down the path of burying structural specifics down in a bunch of extension methods. (Although you could use resources for the paths, it would mean high-friction changes.)

Further, at what point do you stop? For a site of any complexity you're going to need a lot of these helpers.

I've used a somewhat similar approach when referencing stylesheets, javascript, favicons, etc. although I use HtmlHelper rather the UrlHelper extensions as conceptually I think they're more suited to the task. 

Further, because such things are typically added to a master page only, there's no issue with simply passing the complete path into the helper and having it construct an entire tag - rather than just resolve a URI.

(by ChaddeustvanfossonDavid BrownJohn SheehanOskar Austegardderek lawless)

參考文件

  1. MVC Best Practices | Do you build your links into the Url Helper? (CC BY-SA 3.0/4.0)

#asp.net-mvc






相關問題

我返回集合的 Asp.net mvc 操作生成包含 system.linq.Enumerable 的 url,如何刪除它們 (My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them)

在 ASP.NET MVC3 中備份 MySQL 數據庫 (Backup MySQL database in ASP.NET MVC3)

jqgrid將參數傳遞給方法 (jqgrid passing parameter to method)

從局部視圖打開一個彈出窗口並重新加載父局部視圖 (open a popup from partial view and reload the parent partial view)

如何將 MVC 5 項目模板添加到 VS 2012? (How can I add the MVC 5 project template to VS 2012?)

如何避免使用 FirstOrDefault 未將對象引用設置為對象錯誤的實例? (How to avoid Object reference not set to instance of object error with FirstOrDefault?)

ASP.NET MVC:動作內的授權 - 建議的模式或者這是一種氣味? (ASP.NET MVC: Authorization inside an Action - Suggested Patterns or this is a smell?)

MVC 最佳實踐 | 您是否將鏈接構建到 Url Helper 中? (MVC Best Practices | Do you build your links into the Url Helper?)

返回包含 HTML 和 JavaScript 的 PartialView (Returning a PartialView with both HTML and JavaScript)

視圖過濾的表示類與接口 (Presentation class vs Interface for View Filtering)

C# MVC:MVC Html Helpers 與視圖中直接 HTML 的性能和優勢 (C# MVC: Performance and Advantages of MVC Html Helpers vs. Direct HTML in views)

在使用 azure 流量管理器和 azure 應用程序網關與 WAF 時實現國家級阻止 (Achieve country level blocking while using azure traffic manager and azure application gateway with WAF)







留言討論